package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.bean.EnsinoBean;
import br.com.bean.TurnoBean;

public class EnsinoDao {
	
public void inserir (Connection conn, EnsinoBean value) throws Exception{
		
		String sql = "INSERT INTO ensino (id_turno, ensino) VALUES (?,?)";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getTurno().getId());
		ps.setString(2, value.getEnsino());
		ps.execute();
		ps.close();
		
	}
	
	public void alterar (Connection conn, EnsinoBean value) throws Exception{
		
		String sql = "UPDATE ensino SET id_turno=?, ensino=? WHERE id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getTurno().getId());
		ps.setString(2, value.getEnsino());
		ps.execute();
		ps.close();

	}
	
	public void excluir (Connection conn, EnsinoBean value) throws Exception {

		String sql = "DELETE FROM ensino WHERE id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.executeUpdate();
	}
	
	// combo dinamico de turno ensino
		public List<EnsinoBean> getListaEnsino (Connection conn, TurnoBean value) throws Exception{

			String sql = "SELECT * FROM ensino where id_turno = ?";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, value.getId());
			ResultSet rs = ps.executeQuery();
			List<EnsinoBean> listaEnsino = new ArrayList<EnsinoBean>();
			EnsinoBean serie = null;
			while(rs.next()){
				serie = new EnsinoBean();
				serie.setId(rs.getInt("id"));
				serie.setEnsino(rs.getString("ensino"));
				listaEnsino.add(serie);
			}
			return listaEnsino;
		}
	
}
